import 'dart:convert';

import 'package:cjybase/util/time_util.dart';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

extension StringUtil on String {
  /// 非空判断包含null
  bool get isEmpty => this == null || this.isEmpty;

  /// toast提示框
  toast({
    // Duration(seconds: 2) 秒
    Duration duration,
    // EasyLoadingToastPosition.center
    // top,center,bottom,
    EasyLoadingToastPosition toastPosition,
  }) {
    EasyLoading.showToast(this,
        duration: duration, toastPosition: toastPosition);
  }

  /// 打印log
  log({String tag, bool isShowTime = false}) {
    if (tag == null) {
      print(isShowTime ? "[${TimeUtil.nowTime()}]--$this" : this);
    } else {
      print(isShowTime
          ? "[${TimeUtil.nowTime()}]--[$tag]:$this"
          : "[$tag]:$this");
    }
  }

  /// md5 加密
  String get md5Encrypt {
    var content = new Utf8Encoder().convert(this);
    var digest = md5.convert(content);
    // 这里其实就是 digest.toString()
    return hex.encode(digest.bytes);
  }
}
